var ApiUrl = 'http://api.yunyunm.com';
var WebUrl = 'https://www.yunyunm.com';
var kAppKey = 'yymapp';
var kAppSecret = 'siwel2a90hbes61c8yqz3n7hg9kl5a2n';
function showbtnloading(btnid)
{
	var html = '<div class="sk-three-bounce"><div class="sk-child sk-bounce1"></div><div class="sk-child sk-bounce2"></div><div class="sk-child sk-bounce3"></div></div>';
	$api.html($api.byId(btnid),html);
	$api.css($api.byId(btnid),'opacity:0.5');
}

function hidebtnloading(btnid,btnstr)
{
	$api.html($api.byId(btnid),btnstr);
	$api.css($api.byId(btnid),'opacity:1');
}

function showloadingold()
{
	$api.prepend($api.dom('body'),"<div class=\"sk-three-bounce\" id=\"loading\" style=\"padding-top:60px;\"><div class=\"sk-child sk-bounce1\"></div><div class=\"sk-child sk-bounce2\"></div><div class=\"sk-child sk-bounce3\"></div></div>");
}

function loadnextpage()
{
	$api.append($api.dom('body'),"<div id=\"loadnextpage\"><img src=\"../../image/load.gif\"></div>");
}

function allpageload()
{
	$api.append($api.dom('body'),"<div id=\"loadnextpage\">没有更多了...</div>");
}

function removeloadnextpage()
{
	$api.remove($api.byId('loadnextpage'));
}

function noNetwork() {
	//无网络缺省页
	$api.html($api.dom('body'),"<div class=\"sk-three-bounce\" id=\"loading\" style=\"padding-top:3rem;\"><div class=\"sk-child sk-bounce1\"></div><div class=\"sk-child sk-bounce2\"></div><div class=\"sk-child sk-bounce3\"></div></div>");
}

function opNonetwork() {
	openNavWin('#fff','nonetwork_win','widget://html/public/nonetwork_frm.html','网络异常',false,true,false,[],{});
}

function hideloadingold(btnid,btnstr)
{
	$api.remove($api.byId('loading'));
}

function getSign(params, kAppKey, kAppSecret) {
    if (typeof params == "string") {
        return paramsStrSort(params);
    } else if (typeof params == "object") {
        var arr = [];
        for (var i in params) {
            arr.push((i + "=" + params[i]));
        }
        return paramsStrSort(arr.join(("&")));
    }
}

function paramsStrSort(paramsStr) {
    var url = paramsStr;
    var urlStr = url.split("&").sort().join("&");
    var newUrl = urlStr + '&key=' + kAppSecret;
	console.log(newUrl+'!!!');
    return md5(newUrl);
}

function opWin(winName,winUrl,slidback,param,isreload) {
	var delay = 0;
	if(api.systemType != 'ios'){
		delay = 300;
	}
	api.openWin({
		name: winName,
		url: winUrl,
		bounces:false,
		delay: delay,
		reload: isreload,
		vScrollBarEnabled:false,
		slidBackEnabled:slidback,
		progress:{
			type:"page"
		},
		pageParam: param
	});
}

function openNavWin(bgcolor,winName,winUrl,title,hidenav,hidebackbtn,slidback,rbtns,params) {
	var navparam = {
		//background: bgcolor,
		shadow: bgcolor,
		fontSize: 18,
		color: '#000',
		leftButtons: [{
			iconPath: 'widget://image/backa.png'
		}],
		rightButtons: rbtns,
	};
	if(hidebackbtn)
	{
		navparam = {
			//background: bgcolor,
			shadow: bgcolor,
			fontSize: 18,
			color: '#000',
			hideBackButton: true,
			rightButtons: rbtns,
		};
	}
	var param = {
		name: winName,
		url: winUrl,
		bgColor: bgcolor,
		title: title,
		slidBackEnabled:slidback,
		hideNavigationBar: hidenav,
		navigationBar: navparam,
		pageParam: params
	}
	//console.log(JSON.stringify(param));
	api.openTabLayout(param);
}

function toast(msg) {
	api.toast({
		msg: msg,
		duration: 1500,
		location: 'bottom',
		global: true
	});
}

function toasttop(msg) {
	api.toast({
		msg: msg,
		duration: 1500,
		location: 'middle',
		global: true
	});
}

function showtoast(type,str)
{
	var imgpath = "../../image/sq_27.png";
	if(type == 'warning')
	{
		imgpath = "../../image/sq_28.png";
	}
	var htmlstr = "<div class=\"collect_success\"><img src=\""+imgpath+"\" /><b>"+str+"</b></div>";
	$api.prepend($api.dom('body'),htmlstr);
	setTimeout(function() {
		$api.remove($api.dom('.collect_success'));
	}, 1500);
}

function apialert(_msg, callback){
	api.alert({
	    title: '提示',
	    msg: _msg,
	}, function(ret, err) {
		if(callback){
			callback();
		}
	});
}

function tipbox(typeid) {
	var title = '身份认证审核中';
	var msg = '请耐心等待审核结果，谢谢';
	if(typeid == 2){
		title = '身份认证未通过';
		msg = '请重新申请认证，谢谢';
	}
	api.openFrame({
		name: 'tipbox_frm',
		url: 'widget://html/public/tipbox_frm.html',
		bounces: false,
		rect: {
			x: 0,
			y: 0,
			w: 'auto',
			h: 'auto'
		},
		bgColor: 'rgba(0,0,0,0.6)',
		pageParam: {
			title: title,
			msg: msg
		}
	});	
}

function getjsonbykey(jsonObj,domid){
	//查找键盘对应的键盘ID
	var inputid = 0;
	for(var i=0;i<jsonObj.length;i++)
	{
		if(jsonObj[i].name == domid)
		{
			inputid = jsonObj[i].value;
		}

	}
	return inputid;
}

function inputlimit(obj,len) {
	//限制输入框字数
	if(obj.value.length > len)
	{
		obj.value = obj.value.substr(0,len);
	}
}

function checknumletter(value) {
	//判断是否违字母或数字
	var Regx = /^[A-Za-z0-9]*$/;
	if (Regx.test(value)) {
		return true;
	}
	else {
		return false;
	}
}

function formatSeconds(value) {
	var secondTime = parseInt(value);// 秒
	var minuteTime = 0;// 分
	var hourTime = 0;// 小时
	if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
		//获取分钟，除以60取整数，得到整数分钟
		minuteTime = parseInt(secondTime / 60);
		//获取秒数，秒数取佘，得到整数秒数
		secondTime = parseInt(secondTime % 60);
		//如果分钟大于60，将分钟转换成小时
		if(minuteTime > 60) {
			//获取小时，获取分钟除以60，得到整数小时
			hourTime = parseInt(minuteTime / 60);
			//获取小时后取佘的分，获取分钟除以60取佘的分
			minuteTime = parseInt(minuteTime % 60);
		}
	}
	var result = "" + parseInt(secondTime) + "秒";

	if(minuteTime > 0) {
		result = "" + parseInt(minuteTime) + "分" + result;
	}
	if(hourTime > 0) {
		result = "" + parseInt(hourTime) + "小时" + result;
	}
	return result;
}

function Relogin() {
	toast('账号已在其它地方登录！');
	setTimeout(function(){
		openNavWin('#fff','login_win','widget://html/login/login_frm.html','',true,false,false,[],{});
	}, 1000);
}
function showloading() {
	var contentarr = [];
	for(var i=20000; i<20024; i++)
	{
		var curcontent = {"frame":"widget://image/loading/"+i+".png"};
		contentarr.push(curcontent);
	}
	UILoading.keyFrame({
		rect: {
			w: 80,
			h: 100
		},
		styles: {
			bg: 'rgba(0,0,0,0.0)',
			corner: 5,
			interval: 80,
			frame: {
				w: 60,
				h: 60
			}
		},
		content: contentarr,
		//mask: 'rgba(0,0,0,0.5)'       //遮罩层配置，支持#、rgb、rgba，若不传则无遮罩层
	});
}
function hideloading() {
	UILoading.closeKeyFrame();
}